import ActionTypes from '../config/ActionTypes';
import ActionCodes from '../config/ActionCodes';
import { postFetch, getFetch } from '../utils/network/request/HttpExtension.js';
import { createAction } from 'redux-actions';
import Session from '../utils/SessionUtils';
import { AsyncStorage } from 'react-native';

export function GetArticleCommentPager(articleId, currentPage = 0, pageSize = 10) {
	return dispatch => {
		// CMS_GetArticleCommentPager
        dispatch({ type: ActionTypes.COMMENTLIST + ActionTypes.FETCH_LOADING_SUFFIX });
		let param = {
		    SearchType: "0",
		    ARTICLE_ID: articleId,
		    PageIndex: currentPage,
		    PageSize: pageSize,
		    SortBy: "CREATE_DATE DESC"
        };

		postFetch(ActionCodes.commentList, param)
			.then(res => {
                var items = []
                if (res && JSON.stringify(res).indexOf('Public.E') < 0){
                    items = res.List
                }
				dispatch({
					type: ActionTypes.COMMENTLIST + ActionTypes.FETCH_SUCCESS_SUFFIX,
					items: items
				});
			})
			.catch(err => {
				alert(err);
				dispatch({ type: ActionTypes.COMMENTLIST + ActionTypes.FETCH_ERROR_SUFFIX });
			});
	};
}

export function AddArticleComment(articleId, content) {
	console.log(articleId, content)
	// CMS_AddArticleComment
	return dispatch =>{

	dispatch({ type: ActionTypes.COMMENT + ActionTypes.FETCH_LOADING_SUFFIX });
		let param = {
		    ARTICLE_ID: articleId,
		    CONTENT: content,
        };

		postFetch(ActionCodes.commentAdd, param)
			.then(res => {
                var items = []
                // if (res && JSON.stringify(res).indexOf('Public.E') < 0){
                //     items = res.List
                // }
				dispatch({
					type: ActionTypes.COMMENT + ActionTypes.FETCH_SUCCESS_SUFFIX,
				});
			})
			.catch(err => {
				alert(err);
				dispatch({ type: ActionTypes.COMMENT + ActionTypes.FETCH_ERROR_SUFFIX });
			});
					
	}
}
